/**
 * Copyright (c) 2016 Lenovo Group
 * All Rights Reserved.
 * 
 * Project Name:app-gateway
 * Create Time: 2016年2月29日 下午3:26:33
 */
package com.lenovo.lmrp.server.appgateway.spi;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

import org.jboss.resteasy.annotations.GZIP;

import com.lenovo.lmrp.api.appgateway.IStaticResource;
import com.lenovo.lmrp.base.FTPService;
import com.lenovo.lmrp.base.FileSystemService;
import com.lenovo.lmrp.base.IFileService;
import com.lenovo.lmrp.server.appgateway.util.PropertiesUtil;
import com.lenovo.sdc.framework.annotation.RestService;

/**
 * @author zhangzhenga
 *
 */
@RestService
@GZIP
@Path("/app/v1")
public class StaticResourceImpl implements IStaticResource {
    
    

    /* (non-Javadoc)
     * @see com.lenovo.lmrp.api.appgateway.IStaticResource#getStaticCSS(java.lang.String)
     */
    @Override
    public File getStaticCSS(String cssFile) {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.lenovo.lmrp.api.appgateway.IStaticResource#getStaticImage(java.lang.String)
     */
    @Override
    public File getStaticImage(String imgFile) {
        String imagePath = PropertiesUtil.getStringValue("view.imagepath", "");
        URL url = ClassLoader.getSystemResource("");
        String path = new File(url.getFile() + imagePath).getAbsolutePath();
        File image = new File(path,imgFile);
        return image;
    }

    /* (non-Javadoc)
     * @see com.lenovo.lmrp.api.appgateway.IStaticResource#getStaticJS(java.lang.String)
     */
    @Override
    public File getStaticJS(String jsFile) {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.lenovo.lmrp.api.appgateway.IStaticResource#getStaticPage(java.lang.String)
     */
    @Override
    public Response getStaticPage(String pageFile) {
        String fileServerType = PropertiesUtil.getStringValue("file.server.type", "local");
        final String rootPath = PropertiesUtil.getStringValue("file.rootpath", "data");
        
        if (fileServerType.equalsIgnoreCase("local")) {
            
            StreamingOutput stream = new StreamingOutput() {
                @Override
                public void write(OutputStream output) throws IOException, WebApplicationException {
                    IFileService fileService = new FileSystemService(PropertiesUtil.getAttributeBean());
                    fileService.readFile(rootPath + File.separatorChar + pageFile, output);
                    output.flush();
                }
            };
            return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM).build();
        }
        else {
            StreamingOutput stream = new StreamingOutput() {
                @Override
                public void write(OutputStream output) throws IOException, WebApplicationException {
                    IFileService fileService = new FTPService(PropertiesUtil.getAttributeBean());
                    fileService.readFile(rootPath + "/" + pageFile, output);
                    output.flush();
                }
            };
            return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM).build();
        }
    }

}
